<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */

namespace app\common\logic;

use app\common\util\WechatUtil;
use think\Db;
use think\Session;
use think\Image;

class WechatLogic
{
    private $wx_user;
    private $wechatObj;
    
    public function __construct()
    {
        $this->wx_user = M('wx_user')->find();
        if ($this->wx_user['wait_access'] == 0) {
            exit($_GET["echostr"]);
        }
        $this->wechatObj = new WechatUtil($this->wx_user);
    }

    /**
     * 处理接收推送消息
     */
    public function handleMessage()
    {
        $wechatObj = $this->wechatObj;
        $msg = $wechatObj->handleMessage();
        if (!$msg) {
            exit($wechatObj->getError());
        }

        if ($msg['MsgType'] == 'event') {
            if ($msg['Event'] == 'subscribe') {
                $ret = $this->handleSubscribeEvent($msg);//关注事件
                // $this->ajaxReturn($ret);
            } elseif ($msg['Event'] == 'SCAN') {
                //已关注事件
            } elseif ($msg['Event'] == 'CLICK') {
                $this->handleCallService($msg);//点击事件
                // exit();
                // $this->handleClickEvent($msg);//点击事件
            }
        } elseif ($msg['MsgType'] == 'text') {
            $this->handleTextMsg($msg);//用户输入文本
        }
        
        $this->replyDefault($msg);
        // $this->defaultText($msg);
    }
    
    /**
     * 处理关注事件
     * @param array $msg
     * @return type
     */
    private function handleSubscribeEvent($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];
        $openid = $msg['FromUserName'];


        if (!$openid) {
             return ['status' => -1, 'msg' => "openid无效"];
        }

        if ($msg['MsgType'] != 'event' || $msg['Event'] != 'subscribe') {
            return ['status' => 1, 'msg' => "不是关注事件"];
        }
        
        $wechatObj = $this->wechatObj;
        if (!($user = Db::name('users')->where('openid', $openid)->find())) {
            if (false === ($wxdata = $wechatObj->getFanInfo($openid))) {
                return ['status' => -1, 'msg' => $wechatObj->getError()];
            }


            $user = [
                'openid'    => $openid,
                'head_pic'  => $wxdata['headimgurl'],
                'nickname'  => $wxdata['nickname'] ?: '微信用户',
                'sex'       => $wxdata['sex'] ?: 0,
                'subscribe' => $wxdata['subscribe'],
                'oauth'     => 'weixin',
                'reg_time'  => time(),
                'token'     => md5(time().mt_rand(1,99999)),
                'password'  => '',
                'is_distribut' => 1,
            ];
            isset($wxdata['unionid']) && $user['unionid'] = $wxdata['unionid'];
            
            $ret = Db::name('users')->insert($user);
            if (!$ret) {
                $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
                exit($resultStr);
                return ['status' => -1, 'msg' => "保存数据出错"];
            }
        }

        /**
         * 获取在线客服列表,根据会话较少的客服进行优先接入
         */
        //获取在线客服列表
        $online = $wechatObj->getOnlineCustList();
        if($online['kf_online_list'] == []){
            $qrcode_arr = [
                'jmc33666' => '1VH-u9auEgFEUZAj63p_pfaJUnFq7z5Gu46fx2vcJwk',
                'jmcc778' => '1VH-u9auEgFEUZAj63p_pUtDk9Eg8CSINxmF0VOzQK0',
                'jmqb7866' => '1VH-u9auEgFEUZAj63p_pQzVxloA4JzahOhrnnKQK-w',
                'LY66319' => '1VH-u9auEgFEUZAj63p_pWkc3Qbn2Bsm7WnIJDzUZng',
                'BJK57341' => '1VH-u9auEgFEUZAj63p_pdBIRlcQ1D-ubBxn-goNlPU',
                'JMCC98' => '1VH-u9auEgFEUZAj63p_pb5gKBQsByJm7WjUhGk7qXA',
                'JMHB668' => '1VH-u9auEgFEUZAj63p_pZlFlx39Ka7PFZBRDX7myEc',
                'rocket5858' => '1VH-u9auEgFEUZAj63p_pTWn5bSQJ7ipm4F7IwFr6fc',
            ];
            $wechatKey = array_rand($qrcode_arr,1);
            $content = "你好，请问以下有没有最想改善的肌肤困扰呢？
            1：皮肤发黄，干
            2：雀斑，黄褐斑
            3：晒斑，妊娠斑，辐射斑
            4：毛孔粗大，痘痘
            回复数字，添加老师微信扫下方二维码，我都会量肤定制给你最好的建议哦！前100名好友咨询更有免费送298元的吸斑面膜一盒和332元的水光精华2支，送完即止！";
            $wechatObj->sendMsgToOne($msg['FromUserName'],$content);
            // $path = dirname(dirname(dirname(dirname(__FILE__)))).'/public/static/images/mz5884.jpg';
            // $info = $this->wechatObj->uploadMaterial($path, 'image'); 

            $resultStr = $this->wechatObj->createReplyMsgOfImage($toUsername, $fromUsername, $qrcode_arr[$wechatKey]);
            exit($resultStr);
        }


        $accepted_case_arr = [];
        foreach($online['kf_online_list'] as $k=>$v){
            $accepted_case_arr[$k] = $v['accepted_case'];
        }

        $min_acc = min($accepted_case_arr);

        foreach($accepted_case_arr as $ko=>$vo){
            if($vo == $min_acc){
                $kfAccount = $online['kf_online_list'][$ko]['kf_account'];
                $result = $wechatObj->CreateConversation($kfAccount,$openid);

                $content = "Hi，你来了呀~我是吉米祛斑团队的美肤顾问，任何色斑问题找到我就对啦，免费为您贴身定制快速祛斑方案，你还有以下问题困扰吗？1.黄褐斑  2.雀斑 3.妊娠斑 4.老年斑，回复相应的序号，来获得老师的一对一专业指导哦！";
                $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
                exit($resultStr);
            }
        }


        // $wechatObj->sendMsg($openid, "欢迎来到幸福缘商城! 商城入口：".$_SERVER['HTTP_HOST'].'/mobile');
        // 8.31修改回复
        // $wechatObj->sendMsg($openid, "欢迎光临幸福缘商城, 添加客服微信号了解更多 Peimei1212 bvm2357");
        // $content = "欢迎来到幸福缘商城! 商城入口：".$_SERVER['HTTP_HOST'].'/mobile';

        //获取关注回复的图片的media_id
        // $path = dirname(dirname(dirname(dirname(__FILE__)))).'/public/images/peipei.jpg';
        // $info = $this->wechatObj->uploadMaterial($path, 'image');
        // $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $info['media_id']);

    }

    /**
     * 处理点击事件呼唤客服
     * @param type $msg
     */
    private function handleCallService($msg){
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;
        $eventKey = $msg['EventKey'];
        $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $eventKey);
        if($eventKey == 'V1001_CALL_SERVICE'){
            /**
             * 获取在线客服列表,根据会话较少的客服进行优先接入
             */
            $qrcode_arr = [
                'jmc33666' => '1VH-u9auEgFEUZAj63p_pfaJUnFq7z5Gu46fx2vcJwk',
                'jmcc778' => '1VH-u9auEgFEUZAj63p_pUtDk9Eg8CSINxmF0VOzQK0',
                'jmqb7866' => '1VH-u9auEgFEUZAj63p_pQzVxloA4JzahOhrnnKQK-w',
                'LY66319' => '1VH-u9auEgFEUZAj63p_pWkc3Qbn2Bsm7WnIJDzUZng',
                'BJK57341' => '1VH-u9auEgFEUZAj63p_pdBIRlcQ1D-ubBxn-goNlPU',
                 'JMCC98' => '1VH-u9auEgFEUZAj63p_pb5gKBQsByJm7WjUhGk7qXA',
                 'JMHB668' => '1VH-u9auEgFEUZAj63p_pZlFlx39Ka7PFZBRDX7myEc',
                 'rocket5858' => '1VH-u9auEgFEUZAj63p_pTWn5bSQJ7ipm4F7IwFr6fc',
            ];
            //获取在线客服列表
            $online = $wechatObj->getOnlineCustList();
            if($online['kf_online_list'] == []){
                $wechatKey = array_rand($qrcode_arr,1);
                $content = "你好，请问以下有没有最想改善的肌肤困扰呢？
                1：皮肤发黄，干
                2：雀斑，黄褐斑
                3：晒斑，妊娠斑，辐射斑
                4：毛孔粗大，痘痘
                回复数字，添加老师微信扫下方二维码，我都会量肤定制给你最好的建议哦！前100名好友咨询更有免费送298元的吸斑面膜一盒和332元的水光精华2支，送完即止！";
                $wechatObj->sendMsgToOne($msg['FromUserName'],$content);
                // $path = dirname(dirname(dirname(dirname(__FILE__)))).'/public/static/images/mz5884.jpg';
                // $info = $this->wechatObj->uploadMaterial($path, 'image');    
                $resultStr = $this->wechatObj->createReplyMsgOfImage($toUsername, $fromUsername, $qrcode_arr[$wechatKey]);
                exit($resultStr);
            }

            $accepted_case_arr = [];
            foreach($online['kf_online_list'] as $k=>$v){
                $accepted_case_arr[$k] = $v['accepted_case'];
            }

            $min_acc = min($accepted_case_arr);



            foreach($accepted_case_arr as $ko=>$vo){
                if($vo == $min_acc){
                    $kfAccount = $online['kf_online_list'][$ko]['kf_account'];
                    $openid = $msg['FromUserName'];
                    $result = $wechatObj->CreateConversation($kfAccount,$openid);


                    $content = "已为您连接客户服务...\n请问有什么可以帮到您?";
                    $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
                    exit($resultStr);
                }
            }
        }
    }
    
    /**
     * 处理点击事件
     * @param type $msg
     */
    private function handleClickEvent($msg)
    {
        $eventKey = $msg['EventKey'];
        $distribut = tpCache('distribut');
        
        //分销二维码图片
        if ($distribut['qrcode_menu_word'] && $eventKey == $distribut['qrcode_menu_word']) {
            $this->replyMyQrcode($msg);
        }
        
        //其他处理
        $this->handleTextMsg($msg);
    }
    
    /**
     * 回复我的二维码
     */
    private function replyMyQrcode($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;
        
        if (!($user = M('users')->where('openid', $fromUsername)->find())) {
            $content = '请进入商城: '.SITE_URL.' , 再获取二维码哦';
            $reply = $wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
            exit($reply);
        }

        //获取缓存的图片id
        $distribut = tpCache('distribut');
        $mediaId = $this->getCacheQrcodeMedia($user['user_id'], $user['head_pic'], $distribut['qr_big_back']);
        if (!$mediaId) {
            $mediaId = $this->createQrcodeMedia($msg, $user['user_id'], $user['head_pic'], $distribut['qr_big_back']);
        }

        //回复图片消息
        $reply = $wechatObj->createReplyMsgOfImage($toUsername, $fromUsername, $mediaId);
        exit($reply);
    }
    
    private function createQrcodeMedia($msg, $userId, $headPic, $qrBackImg)
    {
        $wechatObj = $this->wechatObj;
        
        //创建二维码关注url
        $qrCode = $wechatObj->createTempQrcode(2592000, $userId);
        if (!(is_array($qrCode) && $qrCode['url'])) {
            $this->replyError($msg, '创建二维码失败');
        }

        //创建分销二维码图片
        $shareImg = $this->createShareQrCode('.'.$qrBackImg, $qrCode['url'], $headPic);
        if (!$shareImg) {
            $this->replyError($msg, '生成图片失败');
        }

        //上传二维码图片
        if (!($mediaInfo = $wechatObj->uploadTempMaterial($shareImg, 'image'))) {
            @unlink($shareImg);
            $this->replyError($msg, '上传图片失败');
        }
        @unlink($shareImg);
        
        $this->setCacheQrcodeMedia($userId, $headPic, $qrBackImg, $mediaInfo);
        
        return $mediaInfo['media_id'];
    }
    
    private function getCacheQrcodeMedia($userId, $headPic, $qrBackImg)
    {
        $symbol = md5("{$headPic}:{$qrBackImg}");
        $mediaIdCache = "distributQrCode:{$userId}:{$symbol}";
        $config = cache($mediaIdCache);
        if (!$config) {
            return false;
        }
        
        //$config = json_decode($config);
        //有效期3天（259200s）,提前5小时(18000s)过期
        if (!(is_array($config) && $config['media_id'] && ($config['created_at'] + 259200 - 18000) > time())) {
            return false;
        }

        return $config['media_id'];
    }
    
    private function setCacheQrcodeMedia($userId, $headPic, $qrBackImg, $mediaInfo)
    {
        $symbol = md5("{$headPic}:{$qrBackImg}");
        $mediaIdCache = "distributQrCode:{$userId}:{$symbol}";
        cache($mediaIdCache, $mediaInfo);
    }
    
    /**
     * 处理点击推送事件
     * @param array $msg
     */
    private function handleTextMsg($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];
        $keyword      = trim($msg['Content']);

        //点击菜单拉取消息时的事件推送 
        /*
         * 1、click：点击推事件
         * 用户点击click类型按钮后，微信服务器会通过消息接口推送消息类型为event的结构给开发者（参考消息接口指南）
         * 并且带上按钮中开发者填写的key值，开发者可以通过自定义的key值与用户进行交互；
         */
        if ($msg['MsgType'] == 'event' && $msg['Event'] == 'CLICK') {
            $keyword = trim($msg['EventKey']);
        }

        if (empty($keyword)) {
            return false;
        }
        
        //分销二维码图片
        // $distribut = tpCache('distribut');
        // if ($distribut['qrcode_input_word'] && $distribut['qrcode_input_word'] == $msg['Content']) {
        //     $this->replyMyQrcode($msg);
        // }
        
        // 图文回复
        // $wx_img = M('wx_img')->where("keyword", "like", "%$keyword%")->find();
        // if( $wx_img) {
        //     $articles[0] = [
        //         'title'       => $wx_img['title'],
        //         'description' => $wx_img['desc'],
        //         'picurl'      => $wx_img['pic'],
        //         'url'         => $wx_img['url']
        //     ];
        //     $resultStr = $this->wechatObj->createReplyMsgOfNews($toUsername, $fromUsername, $articles);
        //     exit($resultStr);
        // }


        // 文本回复
        $wx_text = M('wx_text')->where("keyword", "like", "%$keyword%")->find();
        if ($wx_text) {
            $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $wx_text['text']);
            exit($resultStr);
        }else{
            $this->replyDefault($msg);
        }
    }


    /**
     * 新增永久素材
     * @param array $articles
     *  [
     *      [
     *          "title"=> TITLE,
     *          "thumb_media_id"=> THUMB_MEDIA_ID,
     *          "author"=> AUTHOR,
     *          "digest"=> DIGEST,
     *          "show_cover_pic"=> SHOW_COVER_PIC(0 / 1),
     *          "content"=> CONTENT,
     *          "content_source_url"=> CONTENT_SOURCE_URL
     *      ],
     *      //若新增的是多图文素材，则此处应还有几段articles结构(最多8段)
     *  ]
     * @return MEDIA_ID
     */
    private function addNews(){
        // $fromUsername = $msg['FromUserName'];
        // $toUsername   = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;
        $title = '测试';
        $thumb_media_id = 'b8Wx5RH5nQP3NCxocNRG1PTtuSn9V_innMmpd30NvsY';
        $author = '作者是我';
        $digest = '图文消息的摘要';
        $content = "<a href='http://sex.yougou520.cn/mobile'>商城链接</a>";
        $content_source_url = 'http://shop.yougou520.cn/mobile';
        $articles = [
            [
                "title"=> $title,
                "thumb_media_id"=> $thumb_media_id,
                "author"=> $author,
                "digest"=> $digest,
                "show_cover_pic"=> 1,
                "content"=> $content,
                "content_source_url"=> $content_source_url
            ]
        ];
        $result = $wechatObj->uploadNews($articles);
        // $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $result);
        // exit($resultStr);
        return $result;

    }

    /**
     * 默认回复图文消息
     * @param type $msg
     */
    private function defaultText($msg){
        $openid = $msg['FromUserName'];
        $wechatObj = $this->wechatObj;
        $media_id = 'zGHyeU0F7LTXvE31hETbkV7kqdYQeQdF8Tvw3x9as_E';
        $wechatObj->sendImgMsgToOne($openid,$media_id);
        exit;
    }
    
    /**
     * 默认回复
     * @param type $msg
     */
    private function replyDefault($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];

        // $qrcode_arr = [
        //     'jmc33666' => '1VH-u9auEgFEUZAj63p_pfaJUnFq7z5Gu46fx2vcJwk',
        //     'jmcc778' => '1VH-u9auEgFEUZAj63p_pUtDk9Eg8CSINxmF0VOzQK0',
        //     'jmqb7866' => '1VH-u9auEgFEUZAj63p_pQzVxloA4JzahOhrnnKQK-w',
        //     'LY66319' => '1VH-u9auEgFEUZAj63p_pWkc3Qbn2Bsm7WnIJDzUZng',
        //     'BJK57341' => '1VH-u9auEgFEUZAj63p_pdBIRlcQ1D-ubBxn-goNlPU',
        //      'JMCC98' => '1VH-u9auEgFEUZAj63p_pb5gKBQsByJm7WjUhGk7qXA',
        //      'JMHB668' => '1VH-u9auEgFEUZAj63p_pZlFlx39Ka7PFZBRDX7myEc',
        //      'rocket5858' => '1VH-u9auEgFEUZAj63p_pTWn5bSQJ7ipm4F7IwFr6fc',
        // ];
        // $wechatObj = $this->wechatObj;
        // $wechatKey = array_rand($qrcode_arr,1);
        // $content = "您好，欢迎关注吉米美肤管家，也是你最贴心的肌肤管家，由于在线咨询人数过多，建议添加老师微信：{$wechatKey}，前100名好友咨询更有免费送298元的吸斑面膜一盒和332元的水光精华2支，送完即止！";
        // $wechatObj->sendMsgToOne($msg['FromUserName'],$content);
        // $resultStr = $this->wechatObj->createReplyMsgOfImage($toUsername, $fromUsername, $qrcode_arr[$wechatKey]);
        // exit($resultStr);

        // $path = dirname(dirname(dirname(dirname(__FILE__)))).'/public/static/images/rocket5858.jpg';
        // $info = $this->wechatObj->uploadMaterial($path, 'image');   
        
        // $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $info['media_id'].'456');
        // exit($resultStr);

        $content = '你好，请问以下有没有最想改善的肌肤困扰呢？
        1：皮肤发黄，干
        2：雀斑，黄褐斑
        3：晒斑，妊娠斑，辐射斑
        4：毛孔粗大，痘痘
        回复数字，添加老师微信扫下方二维码，我都会量肤定制给你最好的建议哦！前100名好友咨询更有免费送298元的吸斑面膜一盒和332元的水光精华2支，送完即止！';
        $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        exit($resultStr);


    }
    
    private function replyError($msg, $extraMsg = '')
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername   = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;
        
        if ($wechatObj->isDedug()) {
            $content = '错误信息：';
            $content .= $wechatObj->getError() ?: '';
            $content .= $extraMsg ?: '';
        } elseif ($extraMsg) {
            $content = '系统信息：'.$extraMsg;
        } else {
            $content = '系统正在处理...';
        }
        
        $resultStr = $wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        exit($resultStr);
    }
    
    /**
     * 创建分享二维码图片
     * @param type $backImg 背景大图片
     * @param type $qrText  二维码文本:关注入口
     * @param type $headPic 头像路径
     * @return 图片路径
     */
    private function createShareQrCode($backImg, $qrText, $headPic)
    {
        if (!is_file($backImg) || !$headPic || !$qrText) {
            return false;
        }

        vendor('phpqrcode.phpqrcode');
        vendor('topthink.think-image.src.Image');
        
        $qr_code_path = './public/upload/qr_code/';
        !file_exists($qr_code_path) && mkdir($qr_code_path, 0777, true);

        /* 生成二维码 */
        $qr_code_file = $qr_code_path.time().rand(1, 10000).'.png';
        \QRcode::png($qrText, $qr_code_file, QR_ECLEVEL_M);
        
        $QR = Image::open($qr_code_file);
        $QR_width = $QR->width();
        $QR_height = $QR->height();

        
        /* 添加背景图 */
        if ($backImg && is_file($backImg)) {
            $back =Image::open($backImg);
            $backWidth = $back->width();
            $backHeight = $back->height();
            
            //生成的图片大小以540*960为准
            if ($backWidth <= $backHeight) {
                $refWidth = 540;
                $refHeight = 960;
                if (($backWidth / $backHeight) > ($refWidth / $refHeight)) {
                    $backRatio = $refWidth / $backWidth;
                    $backWidth = $refWidth;
                    $backHeight = $backHeight * $backRatio;
                } else {
                    $backRatio = $refHeight / $backHeight;
                    $backHeight = $refHeight;
                    $backWidth = $backWidth * $backRatio;
                }
            } else {
                $refWidth = 960;
                $refHeight = 540;
                if (($backWidth / $backHeight) > ($refWidth / $refHeight)) {
                    $backRatio = $refHeight / $backHeight;
                    $backHeight = $refHeight;
                    $backWidth = $backWidth * $backRatio;
                } else {
                    $backRatio = $refWidth / $backWidth;
                    $backWidth = $refWidth;
                    $backHeight = $backHeight * $backRatio;
                }
            }
            
            $shortSize = $backWidth > $backHeight ? $backHeight : $backWidth;
            $QR_width = $shortSize / 2;
            $QR_height = $QR_width;
            $QR->thumb($QR_width, $QR_height, \think\Image::THUMB_CENTER)->save($qr_code_file, null, 100);
            $back->thumb($backWidth, $backHeight, \think\Image::THUMB_CENTER)
                ->water($qr_code_file, \think\Image::WATER_CENTER, 90)->save($qr_code_file, null, 100);
            $QR = $back;
        }
        
        /* 添加头像 */
        if ($headPic) {
            //如果是网络头像
            if (strpos($headPic, 'http') === 0) {
                //下载头像
                $ch = curl_init();
                curl_setopt($ch,CURLOPT_URL, $headPic); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                $file_content = curl_exec($ch);
                curl_close($ch);
                //保存头像
                if ($file_content) {
                    $head_pic_path = $qr_code_path.time().rand(1, 10000).'.png';
                    file_put_contents($head_pic_path, $file_content);
                    $headPic = $head_pic_path;
                }
            }
            //如果是本地头像
            if (file_exists($headPic)) {
                $logo = Image::open($headPic);
                $logo_width = $logo->height();
                $logo_height = $logo->width();
                $logo_qr_width = $QR_width / 5;
                $scale = $logo_width / $logo_qr_width;
                $logo_qr_height = $logo_height / $scale;
                $logo_file = $qr_code_path.time().rand(1, 10000);
                $logo->thumb($logo_qr_width, $logo_qr_height)->save($logo_file, null, 100);
                $QR = $QR->water($logo_file, \think\Image::WATER_CENTER);     
                unlink($logo_file);
            }
            if ($head_pic_path) {
                unlink($head_pic_path);
            }
        }
        
        //加上有效时间
        $valid_date = date('Y.m.d', strtotime('+30 days'));
        $QR = $QR->text('有效时间 '.$valid_date, "./vendor/topthink/think-captcha/assets/zhttfs/1.ttf", 16, '#FFFFFF', Image::WATER_SOUTH)->save($qr_code_file);
        
        return $qr_code_file;
    }
}